package org.boulderfoodrescue.webapp.client.old.viewer;


import java.util.Comparator;
import java.util.Date;

import org.boulderfoodrescue.webapp.client.googlemap.GoogleMap;
import org.boulderfoodrescue.webapp.client.old.datamanager.DataManager;
import org.boulderfoodrescue.webapp.client.old.event.PlaceChangedEvent;
import org.boulderfoodrescue.webapp.client.old.event.PlaceChangedHandler;
import org.boulderfoodrescue.webapp.shared.Place;

import com.google.gwt.cell.client.ButtonCell;
import com.google.gwt.cell.client.FieldUpdater;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.maps.client.geom.LatLng;
import com.google.gwt.maps.client.overlay.Marker;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionChangeEvent;
import com.google.gwt.view.client.SingleSelectionModel;

public class PlacesCellTable extends FlowPanel{
  protected CellTable<Place> table;
  protected ListDataProvider<Place> dataProvider;
  protected GoogleMap gMap;
  public PlacesCellTable(final GoogleMap gMap){
    dataProvider = new ListDataProvider<Place>();
    this.gMap = gMap;
    table = new CellTable<Place>();

    TextColumn<Place> nameColumn = new TextColumn<Place>(){
      @Override
      public String getValue(Place object) {
        return object.getName();
      }
    };

    ButtonCell removeCell = new ButtonCell();
    Column<Place, String> removeColumn = new Column<Place, String>(removeCell){
      @Override
      public String getValue(Place object) {
        return "X";
      }
    };

    TextColumn<Place> timeColumn = new TextColumn<Place>(){
      @Override
      public String getValue(Place object) {
        long time = object.getExpiration_date();
        Date date = new Date(time);
        return DateTimeFormat.getFormat(DateTimeFormat.PredefinedFormat.DATE_SHORT).format(date);
      }
    };

    table.addColumn(nameColumn, "Name");
    table.addColumn(timeColumn, "Expires");
    table.addColumn(removeColumn, "Delete");

    dataProvider.addDataDisplay(table);

    ListHandler<Place> sortHandler = new ListHandler<Place>(dataProvider.getList());
    table.addColumnSortHandler(sortHandler);

    nameColumn.setSortable(true);
    sortHandler.setComparator(nameColumn, new Comparator<Place>(){
      @Override
      public int compare(Place o1, Place o2) {
        return o1.getName().compareTo(o2.getName());
      }
    });

    removeColumn.setFieldUpdater(new FieldUpdater<Place, String>(){
      @Override
      public void update(int index, Place object, String value) {
        DataManager.get().fireEvent(new PlaceChangedEvent(object, false));
      }
    });

    final SingleSelectionModel<Place> selectionModel = new SingleSelectionModel<Place>();
    table.setSelectionModel(selectionModel);
    selectionModel.addSelectionChangeHandler(new SelectionChangeEvent.Handler() {
      @Override
      public void onSelectionChange(SelectionChangeEvent event) {
        Place selected = selectionModel.getSelectedObject();
//        Marker marker = gMap.getMarker(selected);
//        gMap.onMarkerClick(marker, selected, false);
      }
    });

    this.add(table);
    table.setWidth("100%");
    for(Place place : DataManager.getLocations())
    {
      addPlace(place);
    }
    DataManager.get().addHandler(new PlaceChangedHandler(){
      @Override
      public void onPlaceAdded(PlaceChangedEvent event) {
        if(!DataManager.getLoginInfo().isLoggedIn())
          addPlace(event.getPlace());
        else if(event.getPlace().getUsername().equals(DataManager.getLoginInfo().getEmailAddress()))
          addPlace(event.getPlace());
        else
        	Window.alert("not added to my pickups "+event.getPlace().getUsername()+" "+DataManager.getLoginInfo().getEmailAddress());
      }

      @Override
      public void onPlaceRemoved(PlaceChangedEvent event) {
        if(!DataManager.getLoginInfo().isLoggedIn())
          removePlace(event.getPlace());
        else if(event.getPlace().getUsername().equals(DataManager.getLoginInfo().getEmailAddress()))
          removePlace(event.getPlace());
      }

      @Override
      public void onPlaceUpdate(PlaceChangedEvent event) {
        if(!DataManager.getLoginInfo().isLoggedIn())
          table.redraw();
        else if(event.getPlace().getUsername().equals(DataManager.getLoginInfo().getEmailAddress()))
          table.redraw();
      }

    }, PlaceChangedEvent.getType());
  }
  private void addPlace(Place new_place){
    dataProvider.getList().add(new_place);
    LatLng latLng = LatLng.newInstance(new_place.getLatitude(), new_place.getLongitude());
//    gMap.addMarker(new_place, latLng, false);
  }
  private void removePlace(Place place){
    dataProvider.getList().remove(place);
//    gMap.removeMarker(place);
  }
  public CellTable<Place> getTable() {
    return table;
  }
  public void setTable(CellTable<Place> table) {
    this.table = table;
  }  
}
